package com.jingtao.product.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jingtao.common.core.domain.R;
import com.jingtao.product.domain.Product;
import com.jingtao.product.domain.Result.CommonResult;
import com.jingtao.product.domain.Result.ProductResult;
import com.jingtao.product.domain.vo.ProductParam;
import com.jingtao.product.domain.vo.QueryProductVo;
import com.jingtao.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName:
 * @Description:
 * @Author: zengfanzhen
 * @Date: 2024/3/26 14:32:40
 */
@RestController
@Slf4j
@Api(tags = "商品管理示例接口")
@RequestMapping("/product")
public class ProductController {


    @Autowired
    private ProductService productService;

    @ApiOperation(value = "获取全部商品列表")
    @RequestMapping(value = "/listAll", method = RequestMethod.POST)
    public R listAll(@RequestBody Product product){
        return R.ok("查询成功");
    }


    @ApiOperation("创建商品")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody ProductParam productParam) {
        int count = productService.create(productParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据商品id获取商品编辑信息")
    @RequestMapping(value = "/updateInfo/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ProductResult> getUpdateInfo(@PathVariable Long id) {
        ProductResult productResult = productService.getUpdateInfo(id);
        return CommonResult.success(productResult);
    }


    @ApiOperation("更新商品")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody ProductParam productParam) {
        int count = productService.update(id, productParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }


    @ApiOperation(value = "根据商品的一些条件参数查询商品信息")
    @RequestMapping(value = "/queryProduct",method = RequestMethod.POST)
    public R getProductById(@RequestBody QueryProductVo queryProductVo){
        log.info("查询商品信息：{}",queryProductVo);
        PageHelper.startPage(queryProductVo.getPageNum(),queryProductVo.getPageSize());
        List<Product> productList = productService.queryProduct(queryProductVo);
        PageInfo<Product> productPageInfo = new PageInfo<>(productList);
        return R.ok(productPageInfo,"查询成功");
    }




}
